package com.ysqcould.web.controller.Community;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ysqcould.Community.domain.SocUsefulActivity;
import com.ysqcould.Community.service.ISocUsefulActivityService;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 活动人员信息Controller
 * 
 * @author jcd
 * @date 2022-09-20
 */
@RestController
@RequestMapping("/Community/activityInfo")
public class SocUsefulActivityController extends BaseController
{
    @Autowired
    private ISocUsefulActivityService socUsefulActivityService;

    /**
     * 查询活动人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('Community:activityInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(SocUsefulActivity socUsefulActivity)
    {
        List strList=new ArrayList<>();
        AjaxResult result=AjaxResult.success();
        startPage();
        List<SocUsefulActivity> list = socUsefulActivityService.selectSocUsefulActivityList(socUsefulActivity);
        final SocUsefulActivity activity = socUsefulActivityService.selectSocUsefulActivityById(socUsefulActivity.getActivityId());
        final JSONArray objects = JSONArray.parseArray(activity.getContent());
        for (int i = 0; i <objects.size() ; i++) {
            Map<Object,Object>map=new LinkedHashMap<>();
            final JSONObject object = objects.getJSONObject(i);
            final String name = object.getString("name");
            map.put("prop", "name"+i);
            map.put("label",name);
            strList.add(map);
        }
        String s = JSON.toJSON(strList).toString();
        System.out.println(s);
        result.put("list",getDataTable(list));
        result.put("strList",s);
        return result;
    }

    /**
     * 导出活动人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('Community:activityInfo:export')")
    @Log(title = "活动人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SocUsefulActivity socUsefulActivity)
    {

        List<SocUsefulActivity> list = socUsefulActivityService.selectSocUsefulActivityList(socUsefulActivity);
        ExcelUtil<SocUsefulActivity> util = new ExcelUtil<SocUsefulActivity>(SocUsefulActivity.class);
        util.exportExcel(response, list, "活动人员信息数据");
    }

    /**
     * 获取活动人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('Community:activityInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(socUsefulActivityService.selectSocUsefulActivityById(id));
    }

    /**
     * 新增活动人员信息
     */
    @PreAuthorize("@ss.hasPermi('Community:activityInfo:add')")
    @Log(title = "活动人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SocUsefulActivity socUsefulActivity)
    {
        return toAjax(socUsefulActivityService.insertSocUsefulActivity(socUsefulActivity));
    }

    /**
     * 修改活动人员信息
     */
    @PreAuthorize("@ss.hasPermi('Community:activityInfo:edit')")
    @Log(title = "活动人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SocUsefulActivity socUsefulActivity)
    {
        return toAjax(socUsefulActivityService.updateSocUsefulActivity(socUsefulActivity));
    }

    /**
     * 删除活动人员信息
     */
    @PreAuthorize("@ss.hasPermi('Community:activityInfo:remove')")
    @Log(title = "活动人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(socUsefulActivityService.deleteSocUsefulActivityByIds(ids));
    }
}
